﻿Namespace Audio

	''' <summary>
	''' Provides a single channel of silenced audio. This is used to fill out MultiPlexingWaveProvider audio ouputs with silence when needed.
	''' </summary>
	''' <remarks></remarks>
	Public Class SilenceWaveProvider
		Implements NAudio.Wave.IWaveProvider
		Public Function Read(buffer() As Byte, offset As Integer, count As Integer) As Integer Implements NAudio.Wave.IWaveProvider.Read
			If Not IsStopped Then
				For n = 0 To count - 1
					buffer(n + offset) = 0
				Next
				Return count
			Else
				Return -1
			End If
		End Function

		Public Sub New()
		End Sub

		Private DefaultSampleRate As Integer = 44100
		Private DefaultBitRate As Integer = 32

		Public Sub New(SampleRate As Integer)
			DefaultSampleRate = SampleRate
		End Sub

		Public Sub New(SampleRate As Integer, BitRate As Integer)
			DefaultSampleRate = SampleRate
			DefaultBitRate = BitRate
		End Sub

		Public Sub New(WF As NAudio.Wave.WaveFormat)
			WaveFormatProperty = WF
		End Sub


		Private WaveFormatProperty As NAudio.Wave.WaveFormat = Nothing
		Public ReadOnly Property WaveFormat As NAudio.Wave.WaveFormat Implements NAudio.Wave.IWaveProvider.WaveFormat
			Get
				If WaveFormatProperty IsNot Nothing Then
					Return WaveFormatProperty
				End If
				Dim wf As New NAudio.Wave.WaveFormat(DefaultSampleRate, DefaultBitRate, 1)
				Return wf
			End Get
		End Property


		Private IsStopped As Boolean = False
		Public Sub [Stop]()
			IsStopped = True
		End Sub

	End Class
End Namespace